using System;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;

namespace Adoor.Wizard
{
	public class CaseSensitiveNameValueSectionHandler : System.Configuration.IConfigurationSectionHandler
	{
		// Methods
		public CaseSensitiveNameValueSectionHandler()
		{
		}

		public static object CreateStatic(object parent, XmlNode section)
		{
			return CaseSensitiveNameValueSectionHandler.CreateStatic(parent, section, "key", "value");
		}

		public static object CreateStatic(object parent, XmlNode section, string keyAttributeName, string valueAttributeName)
		{
			Hashtable htResults = new Hashtable();
			string key;
			string val;
			foreach (XmlNode node1 in section.ChildNodes)
			{
				if(node1 is XmlElement)
				{
					key= node1.Attributes[keyAttributeName].Value;
					val = node1.Attributes[valueAttributeName].Value;
					if (node1.Name == "add")
					{
						htResults[key] = val;
						continue;
					}
					if (node1.Name == "remove")
					{
						htResults.Remove(key);
						continue;
 
					}
					if (node1.Name.Equals("clear"))
					{
						htResults.Clear();
						continue;
 
					}
					throw new Exception(); 
				}
			}
			return htResults;
		}

		// Properties
		protected virtual string KeyAttributeName 
		{ 
			get{ return defaultKeyAttribute; } 
		}

		protected virtual string ValueAttributeName
		{ 
			get{ return defaultValueAttribute; } 
		}

		// Fields
		private const string defaultKeyAttribute = "key";
		private const string defaultValueAttribute = "value";


		#region IConfigurationSectionHandler Members

		public object Create(object parent, object configContext, System.Xml.XmlNode section)
		{
			return CaseSensitiveNameValueSectionHandler.CreateStatic(parent, section, this.KeyAttributeName, this.ValueAttributeName);
		}

		#endregion
	}
}
